import * as THREE from "three";
import { EffectComposer } from "three/addons/postprocessing/EffectComposer.js";
import { RenderPass } from "three/addons/postprocessing/RenderPass.js";
import { ShaderPass } from "three/addons/postprocessing/ShaderPass.js";
import { OutlinePass } from "three/addons/postprocessing/OutlinePass.js";
import { FXAAShader } from "three/addons/shaders/FXAAShader.js";
import { UnrealBloomPass } from "three/addons/postprocessing/UnrealBloomPass.js";
import { OutputPass } from "three/addons/postprocessing/OutputPass.js";

export default class postprocessing {
  constructor(viewerIns, container) {
    this.scene = viewerIns.scene;
    this.camera = viewerIns.camera;
    this.renderer = viewerIns.renderer;
    this.container = container;
    this.selectedObjects = []; //用于存储需要高亮的对象

    this.initComposer();
    this.initOutlinePass();
    this.initFxaaPass();
    this.initUnrealBloomPass();
  }

  initComposer() {
    // 创建效果合成器
    this.composer = new EffectComposer(this.renderer);
    // 添加基础渲染通道（基础场景渲染）
    const renderPass = new RenderPass(this.scene, this.camera);
    this.composer.addPass(renderPass);
  }

  //创建轮廓通道
  initOutlinePass() {
    // 创建轮廓通道
    const outlinePass = new OutlinePass(
      new THREE.Vector2(this.container.value.clientWidth, this.container.value.clientHeight),
      this.scene,
      this.camera,
      this.selectedObjects
    );
    // 设置边框颜色（这里使用亮黄色）
    outlinePass.visibleEdgeColor.set(0xffffff);
    outlinePass.hiddenEdgeColor.set(0x190a05);
    // 配置轮廓效果
    outlinePass.edgeStrength = 3.0; // 边框强度
    outlinePass.edgeGlow = 1; // 边框发光程度
    outlinePass.edgeThickness = 1; // 边框厚度
    outlinePass.usePatternTexture = false; // 是否使用图案纹理
    // 添加轮廓通道到合成器
    this.composer.addPass(outlinePass);
    this.outlinePass = outlinePass;
  }

  // 添加抗锯齿通道
  initFxaaPass() {
    const fxaaPass = new ShaderPass(FXAAShader);
    fxaaPass.material.uniforms["resolution"].value.set(1 / this.container.value.clientWidth, 1 / this.container.value.clientHeight);
    this.composer.addPass(fxaaPass);
    this.fxaaPass = fxaaPass;
  }

  //特效通道：添加Bloom（发光效果）
  initUnrealBloomPass() {
    const unrealBloomPass = new UnrealBloomPass(
      new THREE.Vector2(this.container.value.clientWidth, this.container.value.clientHeight),
      1.5, // 曝光强度
      0.4, // 模糊程度
      0.85 // 混合程度
    );
    // 设置发光参数,阈值、强度和半径。
    unrealBloomPass.threshold = 0;
    unrealBloomPass.strength = 1;
    unrealBloomPass.radius = 0;
    this.composer.addPass(unrealBloomPass);
    //输出通道：将所有处理结果显示到屏幕
    const outputPass = new OutputPass();
    this.composer.addPass(outputPass);
  }

  //更新处理尺寸
  resize(width, height) {
    this.composer.setSize(width, height);
    // 更新FXAA抗锯齿的分辨率
    this.fxaaPass.material.uniforms["resolution"].value.set(1 / width, 1 / height);
  }

  render() {
    this.composer.render();
  }

  //切换对象高亮状态
  toggleOutline(object) {
    if (!this.outlinePass) return;
    // if (selectedObjects.includes(object)) {
    //   // 移除高亮
    //   const index = selectedObjects.indexOf(object);
    //   selectedObjects.splice(index, 1);
    // } else {
    //   // 添加高亮
    //   selectedObjects.push(object);
    // }

    // 更新轮廓通道
    // outlinePass.selectedObjects = selectedObjects;
    this.outlinePass.selectedObjects = [object];
  }
}
